﻿using System;
using System.Drawing;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;

namespace Sample
{
    sealed class Common
    {
        public static readonly Common Instance = new Common();

        private Common()
        {
            ScreenFactor = 1;
            ScreenFactorSuffix = string.Empty;
            ClientRectangle = Screen.PrimaryScreen.WorkingArea;
            int maxScreenSide = Math.Max(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            if(maxScreenSide > 400)
            {
                ScreenFactor = 2;
                ScreenFactorSuffix = "2";
            }
            TouchDevice = SystemSettings.Platform == WinCEPlatform.PocketPC;
            PaintBitmap = new Bitmap(maxScreenSide, maxScreenSide);
            PaintGraphics = Graphics.FromImage(PaintBitmap);
            PaintCopyBitmap = new Bitmap(maxScreenSide, maxScreenSide);
            PaintCopyGraphics = Graphics.FromImage(PaintCopyBitmap);

            Font = new Font("Tahoma", 8.25F, FontStyle.Bold);
            SubFont = new Font("Tahoma", 8F, FontStyle.Regular);
            SmallFont = new Font("Tahoma", 7F, FontStyle.Regular);
            FontBrush = new SolidBrush(SystemColors.WindowText);
            GrayBrush = new SolidBrush(SystemColors.ScrollBar);
            BackBrush = new SolidBrush(SystemColors.Window);
            HighBrush = new SolidBrush(SystemColors.Highlight);
            HighTextBrush = new SolidBrush(SystemColors.HighlightText);
            Pen = new Pen(SystemColors.WindowText, ScreenFactor);
            Pen2 = new Pen(SystemColors.WindowText, 2 * ScreenFactor);
            HighTextPen2 = new Pen(SystemColors.HighlightText, 2 * ScreenFactor);
        }

        public int ScreenFactor { get; set; }
        public bool TouchDevice { get; set; }
        public string ScreenFactorSuffix { get; set; }
        public Bitmap PaintBitmap { get; set; }
        public Graphics PaintGraphics { get; set; }
        public Bitmap PaintCopyBitmap { get; set; }
        public Graphics PaintCopyGraphics { get; set; }
        public Font Font { get; set; }
        public Font SubFont { get; set; }
        public Font SmallFont { get; set; }
        public Brush FontBrush { get; set; }
        public Brush GrayBrush { get; set; }
        public Brush BackBrush { get; set; }
        public Brush HighBrush { get; set; }
        public Brush HighTextBrush { get; set; }
        public Pen Pen { get; set; }
        public Pen Pen2 { get; set; }
        public Pen HighTextPen2 { get; set; }

        private Rectangle clientRectangle;
        public Rectangle ClientRectangle
        {
            get { return clientRectangle; }
            set
            {
                clientRectangle = value;
                clientRectangle.Y = 0;
            }
        }

        public Bitmap LoadBitmap(string imageName, int width, int height)
        {
            return LoadBitmap(this.GetType().Assembly.GetManifestResourceStream("Sample.Images." + imageName + ScreenFactorSuffix + ".png"), width, height);
        }

        public Bitmap LoadBitmap(Stream s, int width, int height)
        {
            Bitmap dib = new Bitmap(s);
            Bitmap ddb = new Bitmap(width * ScreenFactor, height * ScreenFactor);
            Graphics g = Graphics.FromImage(ddb);
            g.DrawImage(dib, new Rectangle(0, 0, width * ScreenFactor, height * ScreenFactor), new Rectangle(0, 0, dib.Width, dib.Height), GraphicsUnit.Pixel);
            dib.Dispose();
            return ddb;
        }

        public string TruncateOneLine(Graphics g, string label, Font font, int totalWidth)
        {
            int width = totalWidth;
            SizeF size = g.MeasureString(label, font);
            if(size.Width < width)
                return label;
            width -= (int)(g.MeasureString("...", font).Width + 0.5);
            string s = label;
            while(size.Width > width)
            {
                s = s.Substring(0, s.Length - 1);
                size = g.MeasureString(s, font);
            }
            while(s.Length > 1 && s.EndsWith(" "))
                s = s.Substring(0, s.Length - 1);

            return s + "...";
        }
    }
}
